/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.lf5.LogLevel;

/**
 *
 * @author Christophe
 */
public class RapportTraitement implements Serializable {
    
    public Logger log = Logger.getLogger(RapportTraitement.class);
    
    private String id = null;
    
    private String titre = null;
    
    private List<String> logs = null;
    
    private boolean doublons = false;
    
    private Map<String, Object> attributes = null;
    
    public RapportTraitement(){
        this(RapportTraitement.class.toString(), "Rapport");
    }
    
    public RapportTraitement(String id, String titre){
        this.id = id;
        this.titre = titre;
    }

    public Logger getLog() {
        return log;
    }

    public void setLog(Logger log) {
        this.log = log;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitre() {
        return titre;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    public List<String> getLogs() {
        if(logs == null){
            logs = new ArrayList<String>();
        }
        return logs;
    }

    public void setLogs(List<String> logs) {
        this.logs = logs;
    }

    public boolean isDoublons() {
        return doublons;
    }

    public void setDoublons(boolean doublons) {
        this.doublons = doublons;
    }

    private Map<String, Object> getAttributes() {
        if(attributes == null){
            attributes = new HashMap<String, Object>();
        }
        return attributes;
    }

    private void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }
    
    public void addAttribute(String id, Object value){
        getAttributes().put(id, value);
    }
    
    public Object getAttributeValue(String id){
        if(!getAttributes().containsKey(id)){
            return null;
        }
        return getAttributes().get(id);
    }
    
    public int incIntegerAttribute(int delta, String id){
        Integer attr = (Integer) getAttributeValue(id);
        if(attr == null){
            attr = new Integer(0);
        }
        
        addAttribute(id, attr.intValue() + delta);
        
        return (Integer) getAttributeValue(id);
    }
    
    public void log(String level, String message){
        
        if(level.equalsIgnoreCase("info")){
            log.info(message);
        }else if(level.equalsIgnoreCase("erreur")){
            log.error(message);
        }else if(level.equalsIgnoreCase("attention")){
            log.warn(message);
        }
        
        message = level.toUpperCase() + ":" + message;
        
        if(doublons || !getLogs().contains(message)){
            getLogs().add(message);   
        }
    }
    
    public void logInfo(String message){
        log("info", message);
    }
    
    public void logErreur(String message){
        log("erreur", message);
    }
    
    public void logWarning(String message){
       log("attention", message);
    }
}
